package com.ambulance.project.biz.service.impl;

import com.ambulance.project.biz.dto.pad.PadUserInfo;
import com.ambulance.project.biz.dto.treat.TreatUserInfo;
import com.ambulance.project.biz.entity.Dispatch;
import com.ambulance.project.biz.entity.Doctor;
import com.ambulance.project.biz.entity.Hospital;
import com.ambulance.project.biz.entity.Nurse;
import com.ambulance.project.biz.enums.Role;
import com.ambulance.project.biz.service.DoctorService;
import com.ambulance.project.biz.service.NurseService;
import com.ambulance.project.biz.service.WebCustomerService;
import com.ambulance.project.biz.util.RtcTokenBuilderSample;
import com.ambulance.project.manager.utils.DoctorAndNurseCommonUtil;
import com.ambulance.project.manager.utils.HelperCommonUtil;
import com.ambulance.project.manager.utils.PadUserLoginResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/***
 *  @description
 *  @author zhao
 *  @date: 2020/5/8
 *  @version: v1.0
 */
@Service(value = "webCustomerService")
public class WebCustomerServiceImpl implements WebCustomerService {


    @Autowired
    private DoctorAndNurseCommonUtil padCommonUtil;
    @Autowired
    private NurseService nurseService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private RtcTokenBuilderSample rtcTokenBuilderSample;


    @Autowired
    private HelperCommonUtil helperCommonUtil;

    @Value("${agora.publicChannel}")
    private String publicChannel;

    @Override
    public TreatUserInfo getTreatUserInfo() {
        Hospital loginResult = helperCommonUtil.getMgtUserLoginResult();
        TreatUserInfo treatUserInfo = TreatUserInfo.fromHospital(loginResult);
        treatUserInfo.setUserMark(loginResult.getHospitalID().toString());
        return treatUserInfo;
    }

    @Override
    public PadUserInfo getUserInfo() {

        PadUserInfo userInfo = new PadUserInfo();

        PadUserLoginResult padLoginUser = padCommonUtil.getPadLoginUser();
        Dispatch dispatch = padCommonUtil.getDispatch();
        Integer userMark = padLoginUser.getUserMark();
        if (padLoginUser.getRole().equals(Role.NURSE.name())) {
            Nurse byId = nurseService.getById(userMark);
            userInfo.setUserMark(padLoginUser.getUserMark().toString());
            userInfo.setDepart(byId.getDepart());
            userInfo.setHospital(padLoginUser.getHospitalName());
            userInfo.setRole(Role.NURSE.name());
            userInfo.setUserName(byId.getName());
            userInfo.setPhone(byId.getPhone());
            userInfo.setImage(byId.getImage());
        } else {
            Doctor byId = doctorService.getById(userMark);
            userInfo.setUserMark(padLoginUser.getUserMark().toString());
            userInfo.setDepart(byId.getDepart());
            userInfo.setHospital(padLoginUser.getHospitalName());
            userInfo.setRole(Role.DOCTOR.name());
            userInfo.setUserName(byId.getName());
            userInfo.setTreatChannel(dispatch.getFlow());
            userInfo.setPhone(byId.getPhone());
            userInfo.setImage(byId.getImage());
        }
        userInfo.setDispatchID(dispatch.getDispatchID().toString());
        userInfo.setTreatChannel(dispatch.getFlow());
        userInfo.setPublicChannel(this.publicChannel);

        return userInfo;
    }

    @Override
    public String getAgoraToken() {
        PadUserInfo userInfo = getUserInfo();
        String token = rtcTokenBuilderSample.getToken(userInfo.getTreatChannel(), userInfo.getPhone());
        return token;
    }

    @Override
    public String getAgoraToken(String channel, String account) {
        return rtcTokenBuilderSample.getToken(channel, account);
    }
}
